home *** CD-ROM | disk | FTP | other *** search
/ Total Java Scripts / Total Java Scripts.iso / JavaApplets / messages / Typewriter / Typewriter.java < prev   
Encoding:
Java Source  |  1998-10-31  |  6.1 KB  |  233 lines

  1. //******************************************************************************
  2. // Typewriter.java: Applet
  3. //
  4. // Created by Ricardo Basto
  5. // July 14, 1997
  6. //
  7. // You are free to use if no modification is done to the original file
  8. //******************************************************************************
  9.  
  10. import java.applet.*;
  11. import java.awt.*;
  12. import java.net.*;
  13.  
  14. public class Typewriter extends Applet implements Runnable
  15. {
  16.     private Thread m_Typewriter = null;
  17.  
  18.     // Members for applet parameters
  19.     // <type>       <MemberVar>        = <Default Value>
  20.     //--------------------------------------------------------------------------
  21.     private    String    m_Text            = "Ricardo Basto's Typewriter|Please e-mail me at|ricbasto@usa.net|";
  22.     private    String    m_Target        = "mailto:ricbasto@usa.net";
  23.     private Color    m_BGColor        = Color.white;
  24.     private Color    m_FGColor        = Color.black;
  25.     private Color    m_HLColor        = Color.blue;
  26.     private int        m_FontSize        = 20;
  27.     private    String    m_FontName        = "Dialog";
  28.     private int        m_Delay            = 100;
  29.  
  30.     // Parameter names
  31.     //--------------------------------------------------------------------------
  32.     private final String PARAM_Text = "Text";
  33.     private final String PARAM_Target = "Target";
  34.     private final String PARAM_BGColor = "BGColor";
  35.     private final String PARAM_FGColor = "FGColor";
  36.     private final String PARAM_HLColor = "HLColor";
  37.     private final String PARAM_FontSize = "FontSize";
  38.     private final String PARAM_FontName = "FontName";
  39.     private final String PARAM_Delay = "Delay";
  40.  
  41.     // Private data members
  42.     //--------------------------------------------------------------------------
  43.     private int StartY = 14;
  44.     private int CurrentY;
  45.     private int lPosition = -1;
  46.     private int LastPos = 0;
  47.  
  48.  
  49.     public String getAppletInfo()
  50.     {
  51.         return "Name: Typewriter\r\n" +
  52.                "Author: Ricardo Basto\r\n" +
  53.                "Created with Microsoft Visual J++ Version 1.1";
  54.     }
  55.  
  56.     public String[][] getParameterInfo()
  57.     {
  58.         String[][] info =
  59.         {
  60.             { PARAM_Text, "String", "The text to be typed" },
  61.             { PARAM_Target, "String", "Target URL" },
  62.             { PARAM_BGColor, "int", "Background color" },
  63.             { PARAM_FGColor, "int", "Foreground color" },
  64.             { PARAM_HLColor, "int", "Highlight color" },
  65.             { PARAM_FontSize, "int", "Font size in pixels" },
  66.             { PARAM_FontName, "String", "Font's typeface name" },
  67.             { PARAM_Delay, "int", "Typying delay" },
  68.         };
  69.         return info;
  70.     }
  71.  
  72.     public void init()
  73.     {
  74.         String param;
  75.  
  76.         // Text: The text to be typed
  77.         //----------------------------------------------------------------------
  78.         param = getParameter(PARAM_Text);
  79.         if (param != null)
  80.             m_Text = param;
  81.  
  82.         // Text: Target URL
  83.         //----------------------------------------------------------------------
  84.         param = getParameter(PARAM_Target);
  85.         if (param != null)
  86.             m_Target = param;
  87.  
  88.         // BGColor: Background color
  89.         //----------------------------------------------------------------------
  90.         param = getParameter(PARAM_BGColor);
  91.         if (param != null)
  92.             m_BGColor = new Color(Integer.parseInt(param));
  93.  
  94.         // FGColor: Foreground color
  95.         //----------------------------------------------------------------------
  96.         param = getParameter(PARAM_FGColor);
  97.         if (param != null)
  98.             m_FGColor = new Color(Integer.parseInt(param));
  99.  
  100.         // HLColor: Highlight color
  101.         //----------------------------------------------------------------------
  102.         param = getParameter(PARAM_HLColor);
  103.         if (param != null)
  104.             m_HLColor = new Color(Integer.parseInt(param));
  105.  
  106.         // FontSize: Font size in pixels
  107.         //----------------------------------------------------------------------
  108.         param = getParameter(PARAM_FontSize);
  109.         if (param != null)
  110.             m_FontSize = Integer.parseInt(param);
  111.  
  112.         // FontName: Font's typeface name
  113.         //----------------------------------------------------------------------
  114.         param = getParameter(PARAM_Text);
  115.         if (param != null)
  116.             m_FontName = param;
  117.  
  118.         // Delay: Typying delay
  119.         //----------------------------------------------------------------------
  120.         param = getParameter(PARAM_Delay);
  121.         if (param != null)
  122.             m_Delay = Integer.parseInt(param);
  123.  
  124.  
  125.         // Parameter Validation
  126.         //----------------------------------------------------------------------
  127.         if (m_Delay < 1) m_Delay = 1;
  128.         if (m_FontSize < 2) m_FontSize = 2;
  129.  
  130.  
  131.         // Initialization Code
  132.         //----------------------------------------------------------------------
  133.         StartY = m_FontSize + 2;
  134.         CurrentY = StartY;
  135.  
  136.         setForeground(m_FGColor);
  137.         setBackground(m_BGColor);
  138.         setFont(new Font(m_FontName, Font.BOLD, m_FontSize));
  139.     }
  140.  
  141.  
  142.     // All drawing code is placed in the paint event
  143.     //--------------------------------------------------------------------------
  144.     public void paint(Graphics g)
  145.     {
  146.         if (m_Text.charAt(lPosition) == '|')
  147.         {
  148.             CurrentY-=2;
  149.             if (CurrentY > -3) 
  150.                 lPosition--;
  151.             else
  152.             {
  153.                 CurrentY = StartY;
  154.                 LastPos = ++lPosition;
  155.             }
  156.         }
  157.  
  158.         g.drawString(m_Text.substring(LastPos, lPosition+1), 0, CurrentY);
  159.     }
  160.  
  161.     public void start()
  162.     {
  163.         if (m_Typewriter == null)
  164.         {
  165.             m_Typewriter = new Thread(this);
  166.             m_Typewriter.start();
  167.         }
  168.     }
  169.     
  170.     public void stop()
  171.     {
  172.         if (m_Typewriter != null)
  173.         {
  174.             m_Typewriter.stop();
  175.             m_Typewriter = null;
  176.         }
  177.     }
  178.  
  179.     // All string manipulation code is placed in the thread
  180.     //--------------------------------------------------------------------------
  181.     public void run()
  182.     {
  183.         while (true)
  184.         {
  185.             try
  186.             {
  187.                 lPosition++;
  188.                 if (lPosition > m_Text.length()) 
  189.                 {
  190.                     lPosition = -1;
  191.                     LastPos = 0;
  192.                     Thread.sleep(m_Delay * 10);
  193.                 }
  194.                 else
  195.                 {
  196.                     repaint();
  197.  
  198.                     Thread.sleep(m_Delay);
  199.                 }
  200.             }
  201.             catch (InterruptedException e)
  202.             {
  203.                 stop();
  204.             }
  205.         }
  206.     }
  207.  
  208.     public boolean mouseDown(Event evt, int x, int y)
  209.     {
  210.         try
  211.         {
  212.             getAppletContext().showDocument(new URL(m_Target));
  213.         }
  214.         catch (MalformedURLException e)
  215.         {
  216.             m_Text = "Error|MalformedURLException|Target URL invalid|";
  217.         }
  218.         return true;
  219.     }
  220.  
  221.     public boolean mouseEnter(Event evt, int x, int y)
  222.     {
  223.         setForeground(m_HLColor);
  224.         return true;
  225.     }
  226.  
  227.     public boolean mouseExit(Event evt, int x, int y)
  228.     {
  229.         setForeground(m_FGColor);
  230.         return true;
  231.     }
  232. }
  233.